<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.*,com.tianxiabuyi.db.*,com.tianxiabuyi.model.*,com.tianxiabuyi.util.*"%>
<%@ page import="org.jsoup.Jsoup,org.jsoup.nodes.Element,org.jsoup.select.Elements"%>
<%@ page import="com.google.gson.reflect.TypeToken"%>
<%@ page import="org.dom4j.*"%>
<%!
	public Integer NEWS_CATEGORY = 1;   // 新闻
	public Integer QUEST_CATEGORY = 2;  // 问答
	public Integer DOCTOR_CATEGORY = 3; // 医生
	public Integer COMMENT_CATEGORY = 4; // 评论
	public Integer SEE = 1; // 浏览
	public Integer ATTENTION = 2; // 关注
	public Integer LOVE = 3; // 点赞
	public Integer TREAD = 4; // 踩
	public Integer CANCEL_LOVE = 5; // 取消点赞
	public Integer CANCEL_TRRAD = 6; // 取消踩
	// 操作记录 cancel为true是取消false为增加
	public Message operate(OperateHistory oh, boolean cancel) {	
		List<OperateHistory> ohs = new ArrayList<OperateHistory>();
		Integer id = null;
		if (cancel) {  // 取消收藏、关注
			ohs = QBuilder.db("basedb").select().from("OperateHistory").where(new Where("id=", oh.getId()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).page(1,1).query().getResultObj();
			if (ohs.size() == 0) {
				return new Message(30091, "没有相关记录");
			} 
			if (oh.getHospital() == 1068) {
				score(oh, cancel);
			}
			oh = ohs.get(0);
			oh.setStatus(STATUS_DISABLE);
			id = oh.getId();
			DBTools.db("basedb").modItem(oh, oh.getId());
		} else {
			// 访问量不限制次数
			if (SEE.equals(oh.getOperate())) {
				id = (Integer)DBTools.db("basedb").addItem(oh);
			} else {
				ohs = QBuilder.db("basedb").select().from("OperateHistory").where(new Where("uid=", oh.getUid()).and("oid=", oh.getOid()).and("category=", oh.getCategory()).and("operate=", oh.getOperate()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).page(1,1).query().getResultObj();
				if (ohs.size() == 0) {
					id = (Integer)DBTools.db("basedb").addItem(oh);
					if (oh.getHospital() == 1068) {
						score(oh, cancel);
					}
				} else {
					if (!SEE.equals(oh.getOperate())) // 如果是重复浏览则跳过失败操作
						return new Message(30091, "该操作不能重复执行");
				}
			}
		}
		Message msg = new Message();
		msg.put("id", id);
		return msg;
	}
	
	
	// 查询浏览次数、关注次数、点赞次数、踩次数
	public List query(List list, OperateHistory oh) {
		String ids = "'-9999999'";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			String id = Tools.getDesc(map.get("id"), "");
			if ("".equals(id)) continue;
			ids += ",'" + id + "'";
		}
		List counts = QBuilder.db("basedb").select("new map(count(id) as count, oid as oid, operate as operate)").from("OperateHistory").where(new Where("oid in(" + ids +")").and("category=", oh.getCategory()).and("status=", STATUS_ENABLE).and("hospital=", oh.getHospital())).group("oid, operate").query().getResultObj();
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			map.put("browse", 0);
			map.put("attention", 0);
			map.put("love", 0);
			map.put("tread", 0);
			String id = Tools.getDesc(map.get("id"), "");
			Iterator it = counts.iterator();
			while (it.hasNext()) {
				Map temp = (Map)it.next();
				String tempId = Tools.getDesc(temp.get("oid"), "----");
				if (id.equals(tempId)) {
					Integer operate = Tools.getInt(temp.get("operate") + "", -1);
					if (operate == -1) continue;
					String key = operate.equals(SEE) ? "browse" : operate.equals(ATTENTION) ? "attention" :operate.equals(LOVE) ?  "love":"tread";
					map.put(key, Tools.getLong(temp.get("count") + "", 0L));
					it.remove();
				}
			}
		}
		return list;
	}	
	
	//  查询是否已经赞过或踩过(对新闻、问题或者评论)
	public List<Map> love(List<Map> list, Integer uid, OperateHistory oh) {
		Map<Integer, String[]> opMap = new HashMap<Integer, String[]>();
		opMap.put(TREAD, new String[]{"is_treaded", "treaded_id"});
		opMap.put(LOVE, new String[]{"is_loved", "loved_id"});
		Set<Integer> set = opMap.keySet();
		for (Integer key:set) {
			List<Map> oo = QBuilder.db("basedb").select("new map(oid as oid,id as id)").from("OperateHistory").where(new Where("uid=", uid).and("category=", oh.getCategory()).and("status=", STATUS_ENABLE).and("operate=", key)).query().getResultObj();
			List<String> oids = new ArrayList<String>();
			Map<String, Integer> idMap = new HashMap<String, Integer>();
			for (Map map:oo) {
				oids.add((String)map.get("oid"));
				idMap.put(map.get("oid") + "", (Integer)map.get("id"));
			}
			for (Map map:list) {
				String id = Tools.getDesc(map.get("id")); 
				if (oids.contains(id)) {
					map.put(opMap.get(key)[0], 1);
					map.put(opMap.get(key)[1], idMap.get(id));
				} 
				else 
					map.put(opMap.get(key)[0], 0);
			}
		}
		return list;
	}
	
	// 对回答的加减分
	public void score(OperateHistory oh, boolean cancel) {
		if (COMMENT_CATEGORY.equals(oh.getCategory())) {
			Integer id = oh.getId();
			String uid = oh.getUid();
			String oid = oh.getOid();	
			
			Integer operate = oh.getOperate();
			List<Map<String, Integer>> list = QBuilder.db("basedb").select("new map(q.uid as uid, c.uid as cuid,c.id as cid)").from("Quest as q, Comment as c, OperateHistory as o").where(new Where("o.id=", id).and("q.id = c.srcId").and("o.oid=c.id")).query().getResultObj();
			
			if (list.size() == 0) {
				return;
			}
			Map sMap = list.get(0);
			Map scoreMap = new HashMap();
			scoreMap.put("hospital", oh.getHospital());
			scoreMap.put("uid", sMap.get("cuid"));
			scoreMap.put("status", STATUS_ENABLE);
			scoreMap.put("json", "{}");
			scoreMap.put("create_time", System.currentTimeMillis());
			scoreMap.put("activity_id", sMap.get("cid"));
			scoreMap.put("operator", uid + "");
			scoreMap.put("category", COMMENT_CATEGORY);
			// 取消赞或踩
			if (cancel) {
				scoreMap.put("type", operate.equals(LOVE)?CANCEL_LOVE:CANCEL_TRRAD);
				if (Tools.getInt(uid).equals(sMap.get("uid"))) {
					scoreMap.put("score", operate.equals(LOVE)?-10:+5);
				} else {
					scoreMap.put("score", operate.equals(LOVE)?-5:+5);
				}
			}
			// 赞或踩
			if (!cancel) {
				scoreMap.put("type", operate.equals(LOVE)?LOVE:TREAD);
				if (Tools.getInt(uid).equals(sMap.get("uid"))) {
					scoreMap.put("score", operate.equals(LOVE)?+10:-5);
				} else {
					scoreMap.put("score", operate.equals(LOVE)?+5:-5);
				}
			}
			// 社区评论点赞不加分
			List<Map> srcList = QBuilder.db("basedb").queryHql("select new map(srcId as srcid) from Comment where id='"+oid+"' and status='"+STATUS_ENABLE+"'");
			if(srcList.size() != 0) {
				Map srcMap = srcList.get(0);
				Integer srcid = Tools.getInt(srcMap.get("srcid")+"", -11);
				List<Map> queList = QBuilder.db("basedb").queryHql("select new map(groupId as gids) from Quest where id='"+srcid+"' and status='"+STATUS_ENABLE+"'");
				if(queList.size() != 0) {
					Map gidsMap = queList.get(0);
					Integer gids = Tools.getInt(gidsMap.get("gids")+"", -11);
					if((gids == 2) || (gids == 3) || (gids == 4)) {
						DBTools.db("basedb").addItem("user_score", scoreMap);
					}
				}
			}
		}
	}
%>